// 我的-部门管理-邮件通知页面

const app = getApp();
const db = wx.cloud.database()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    subject: "",
    context: "",
    checkedArray: []
  },
  temperSave() {
    console.log(app.personInfo._openid);
    db.collection("emaiContentTemp").where({
      _openid: app.personInfo._openid
    }).get().then(res => {
      console.log(res);
      if (res.data.length == 0) {
        db.collection("emaiContentTemp").add({
          data: {
            subject: this.data.subject,
            context: this.data.context
          }
        })
      } else {
        db.collection("emaiContentTemp").where({
          _openid: app.personInfo._openid,
        }).update({
          data: {
            subject: this.data.subject,
            context: this.data.context
          }
        })
      }
    })
  },
  getTemperSave() {
    db.collection("emaiContentTemp").where({
      _openid: app.personInfo._openid
    }).get().then(res => {
      console.log(res.data[0]);
      var {
        subject,
        context
      } = res.data[0];
      this.setData({
        subject,
        context
      })
    })
  },
  subject(event) {
    var {
      value
    } = event.detail
    console.log(value);
    this.setData({
      subject: value
    })
  },
  context(event) {
    var {
      value
    } = event.detail
    console.log(value);
    this.setData({
      context: value
    })
  },
  async sendEmail() {
    if (this.data.subject != '' || this.data.context != '') {
      wx.showModal({
        title: '提示',
        content: '确定通知内容无误且发送邮件吗'
      }).then(async res => {
        if (res.confirm) {
          wx.showLoading({
            title: '发送中',
          })
          for (let val of this.data.checkedArray) {
            var that = this
            var context = this.data.context
            await wx.cloud.callFunction({
              // 要调用的云函数名称
              name: 'sendEmail',
              // 传递给云函数的 event 参数
              data: {
                toEmail: val.email,
                realName: val.realName,
                context,
                subject: this.data.subject
              }
            }).then(res => {
              // output: res.result === 3
              console.log(res);
            }).catch(err => {
              // handle error
              console.log(err);

            })
          }
          wx.hideLoading({
            success: (res) => {},
          })
          wx.showModal({
            title: '提示',
            content: "发送成功!",
            showCancel: false
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '请完善内容',
        showCancel: false
      })
    }





  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var checkedArray = JSON.parse(options.checkedArray)
    console.log(checkedArray);
    this.data.tem = checkedArray
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      checkedArray: this.data.tem
    })
    this.getTemperSave();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})